﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Data;

namespace CBMS.Common
{
    public class DBConvert
    {
        private static CultureInfo cult = new CultureInfo("en-US", false);
        private const int INT_MIN_VALUE = int.MinValue;
        private const short SHORT_MIN_VALUE = short.MinValue;
        private const long LONG_MIN_VALUE = long.MinValue;
        private const double DOUBLE_MIN_VALUE = double.MinValue;
        private const decimal DECIMAL_MIN_VALUE = decimal.MinValue;
        private static DateTime DATETIME_MIN_VALUE = DateTime.MinValue;
        private static char CHAR_MIN_VALUE = char.MinValue;
        private static string PARAM_SEPARATOR = ",";

        public static object ParseBoolToDB(bool value)
        {
            try
            {
                return (object)value;
            }
            catch { return false; }
        }
        public static object ParseShortToDB(short value)
        {
            return (value == SHORT_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseIntToDB(int value)
        {
            return (value == INT_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseLongToDB(long value)
        {
            return (value == LONG_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseDoubleToDB(double value)
        {
            return (value == DOUBLE_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseDecimalToDB(decimal value)
        {
            return (value == DECIMAL_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseDateTimeToDB(DateTime value)
        {
            return (value == DATETIME_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseStringToDB(string value)
        {
            return (value == null || value == string.Empty) ? DBNull.Value : (object)value;
        }
        public static object ParseCharToDB(char value)
        {
            return (value == CHAR_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        //
        public static bool ParseDBToBool(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ? false : (bool)(dre[column]);
        }

        public static int ParseDBToInt(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(dre.GetOrdinal(column));
            string val = string.Empty;
            if (has == false)
            {
                val = dre.GetInt32(index).ToString();
            }
            return has ? INT_MIN_VALUE : int.Parse(val);
        }
        public static short ParseDBToShort(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ? Int16.MinValue : Int16.Parse(dre[column].ToString());
        }

        public static int ParseDBToSmallInt(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(dre.GetOrdinal(column));
            string val = string.Empty;
            if (has == false)
            {
                val = dre.GetInt16(index).ToString();
            }
            return has ? INT_MIN_VALUE : int.Parse(val);
        }

        public static short ParseDBToTinyInt(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(dre.GetOrdinal(column));
            string val = string.Empty;
            if (has == false)
            {
                val = dre.GetByte(index).ToString();
            }
            return has ? SHORT_MIN_VALUE : short.Parse(val);
        }

        public static int ParseDBToBigInt(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
            INT_MIN_VALUE : int.Parse(dre.GetInt64(dre.GetOrdinal(column)).ToString());
        }
        public static long ParseDBToLong(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(index);
            return has ?
              LONG_MIN_VALUE : dre.GetInt64(dre.GetOrdinal(column));
        }
        public static double ParseDBToDouble(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
              DOUBLE_MIN_VALUE : dre.GetDouble(dre.GetOrdinal(column));
        }
        public static decimal ParseDBToDecimal(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
              DECIMAL_MIN_VALUE : dre.GetDecimal(dre.GetOrdinal(column));
        }
        public static DateTime ParseDBToDateTime(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
              DATETIME_MIN_VALUE : dre.GetDateTime(dre.GetOrdinal(column));
        }
        public static string ParseDBToString(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
              string.Empty : dre.GetString(dre.GetOrdinal(column));
        }
        public static string ParseDBToGUI(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(index);
            return has ?
            string.Empty : dre.GetGuid(index).ToString();
        }

        public static char ParseDBToChar(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
            CHAR_MIN_VALUE : char.Parse(dre.GetString(dre.GetOrdinal(column)));
        }
        //
        public static string ParseString(int value)
        {
            return (value == INT_MIN_VALUE) ? string.Empty : value.ToString();
        }
        public static string ParseString(long value)
        {
            return (value == LONG_MIN_VALUE) ? string.Empty : value.ToString();
        }
        public static string ParseString(double value)
        {
            return (value == DOUBLE_MIN_VALUE) ? string.Empty : value.ToString();
        }
        //public static string ParseString(decimal value)
        //{ 
        //  return ( value ==  DECIMAL_MIN_VALUE)? string.Empty : value.ToString(); 
        //}
        public static string ParseString(decimal _value)
        {
            if (_value < 0)
            {
                return string.Empty;
            }
            //cult.NumberFormat.NumberDecimalDigits = 2;
            NumberFormatInfo nfi = cult.NumberFormat;
            return _value.ToString("N", nfi) + " " + nfi.CurrencySymbol;
        }
        public static string ParseString(DateTime _value)
        {
            if (_value == DateTime.MinValue)
            {
                return string.Empty;
            }
            //cult.NumberFormat.NumberDecimalDigits = 2;
            DateTimeFormatInfo nfi = cult.DateTimeFormat;
            return _value.ToString("F", nfi);
        }
        /// <summary>
        /// Convert datetime to string by CultureInfo en-US
        /// </summary>
        /// <param name="_value"></param>
        /// <param name="_format">Format pattern: d, D, f, F, g, G... </param>
        /// <returns></returns>
        public static string ParseString(DateTime _value, string _format)
        {
            if (_value == DateTime.MinValue)
            {
                return string.Empty;
            }
            //cult.NumberFormat.NumberDecimalDigits = 2;
            DateTimeFormatInfo nfi = cult.DateTimeFormat;
            return _value.ToString(_format, nfi);
        }

        public static string ParseString(char value)
        {
            return (value == CHAR_MIN_VALUE) ? string.Empty : value.ToString();
        }
        //
        public static int ParseInt(string value)
        {
            int result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = int.Parse(value); }
            catch { result = INT_MIN_VALUE; }
            return result;
        }
        public static short ParseShort(string value)
        {
            short result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = short.Parse(value); }
            catch { result = SHORT_MIN_VALUE; }
            return result;
        }
        public static long ParseLong(string value)
        {
            long result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = long.Parse(value); }
            catch { result = LONG_MIN_VALUE; }
            return result;
        }
        public static double ParseDouble(string value)
        {
            double result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = double.Parse(value); }
            catch { result = DOUBLE_MIN_VALUE; }
            return result;
        }
        public static double ParseDoubleDiv100(string value)
        {
            double result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try
            {
                result = double.Parse(value);
                result /= 100;
            }
            catch { result = DOUBLE_MIN_VALUE; }
            return result;
        }
        public static decimal ParseDecimal(string value)
        {
            decimal result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = decimal.Parse(value); }
            catch { result = DECIMAL_MIN_VALUE; }
            return result;
        }

        public static DateTime ParseDateTime(string value)
        {
            DateTime result = DATETIME_MIN_VALUE;
            if (value != null && value.Trim() != string.Empty && value.Length == 10)
            {
                try
                {
                    DateTimeFormatInfo nfi = cult.DateTimeFormat;
                    nfi.ShortDatePattern = "dd/MM/yyyy";
                    result = DateTime.Parse(value, nfi);
                }
                catch { result = DATETIME_MIN_VALUE; }
            }
            return result;
        }

        public static DateTime ParseDateTime(string value, string formatType)
        {
            DateTime result = DATETIME_MIN_VALUE;
            if (value != null && value.Trim() != string.Empty && (value.Length >= 8 && value.Length <= 10))
            {
                try
                {
                    DateTimeFormatInfo nfi = cult.DateTimeFormat;
                    nfi.ShortDatePattern = formatType;
                    result = DateTime.Parse(value, nfi);
                }
                catch { result = DATETIME_MIN_VALUE; }
            }
            return result;
        }

        public static long ParseLongMul1000(string value)
        {
            long result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try
            {
                result = long.Parse(value);
                result *= 1000;
            }
            catch { result = LONG_MIN_VALUE; }
            return result;
        }
    }
}